﻿using System;
using Autofac;
using Cloak.Mvvm.Navigation;

namespace Cloak.Autofac.Navigation
{
	public class NavigationModule : BuilderModule
	{
		public NavigationModule()
		{
			Register(c => new CachingPageModelMap(new AttributedPageModelMap()))
			.As<IPageModelMap>()
			.SingleInstance();

			// The instance of ILifetimeScope is the application's container, which is automatically registered by Autofac.
			//
			// PageModelScope exposes the ability to create child scopes, which NavigationModel will do whenever navigation to a new page occurs.
			//
			// This ultimately resolves the page's view model, and all its dependencies, in its own dedicated container.

			Register(c => new PageModelScope(c.Resolve<ILifetimeScope>()))
			.As<IPageModelScope>()
			.SingleInstance();

			RegisterType<NavigationModel>().As<NavigationModel, INavigationContext>().SingleInstance();
		}
	}
}